# Carnal Realms negotiated-marriage and ceremony effects.

cr_clear_marriage_term_flags_effect = {
	clr_character_flag = cr_marriage_term_patrilineal
	clr_character_flag = cr_marriage_term_matrilineal
	clr_character_flag = cr_marriage_author_keep_religion
	clr_character_flag = cr_marriage_author_personal_conversion
	clr_character_flag = cr_marriage_author_realm_conversion
	clr_character_flag = cr_marriage_recipient_keep_religion
	clr_character_flag = cr_marriage_recipient_personal_conversion
	clr_character_flag = cr_marriage_recipient_realm_conversion
	clr_character_flag = cr_marriage_author_stays_independent
	clr_character_flag = cr_marriage_author_offers_vassalization
}

cr_clear_marriage_negotiation_effect = {
	if = {
		limit = { event_target:cr_marriage_proposer = { always = yes } }
		event_target:cr_marriage_proposer = {
			clr_character_flag = cr_marriage_negotiation_active
			cr_clear_marriage_term_flags_effect = yes
		}
	}
	if = {
		limit = { event_target:cr_marriage_candidate = { always = yes } }
		event_target:cr_marriage_candidate = {
			clr_character_flag = cr_marriage_negotiation_active
			cr_clear_marriage_term_flags_effect = yes
		}
	}
	clear_event_target = cr_marriage_offer_author
	clear_event_target = cr_marriage_offer_recipient
	clear_event_target = cr_marriage_proposer
	clear_event_target = cr_marriage_candidate
}

cr_close_failed_marriage_proposal_effect = {
	if = {
		limit = { event_target:cr_marriage_proposer = { always = yes } }
		event_target:cr_marriage_proposer = {
			set_character_flag = cr_marriage_proposal_cooldown
			character_event = { id = cr_marriage.103 days = 90 }
		}
	}
	cr_clear_marriage_negotiation_effect = yes
}

# Conversion may run in only one direction within a proposal. The event pages
# enforce that rule, and settlement validation checks it again before applying
# either the personal or realm-wide result.
cr_apply_negotiated_religion_terms_effect = {
	if = {
		limit = {
			event_target:cr_marriage_offer_author = {
				OR = {
					has_character_flag = cr_marriage_author_personal_conversion
					has_character_flag = cr_marriage_author_realm_conversion
				}
			}
		}
		event_target:cr_marriage_offer_author = {
			religion = event_target:cr_marriage_offer_recipient
		}
	}
	else_if = {
		limit = {
			event_target:cr_marriage_offer_author = {
				OR = {
					has_character_flag = cr_marriage_recipient_personal_conversion
					has_character_flag = cr_marriage_recipient_realm_conversion
				}
			}
		}
		event_target:cr_marriage_offer_recipient = {
			religion = event_target:cr_marriage_offer_author
		}
	}

	if = {
		limit = {
			event_target:cr_marriage_offer_author = {
				has_character_flag = cr_marriage_author_realm_conversion
			}
		}
		event_target:cr_marriage_offer_author = {
			any_realm_character = {
				limit = { is_landed = yes }
				religion = event_target:cr_marriage_offer_author
			}
			any_realm_province = { religion = event_target:cr_marriage_offer_author }
		}
	}
	if = {
		limit = {
			event_target:cr_marriage_offer_author = {
				has_character_flag = cr_marriage_recipient_realm_conversion
			}
		}
		event_target:cr_marriage_offer_recipient = {
			any_realm_character = {
				limit = { is_landed = yes }
				religion = event_target:cr_marriage_offer_recipient
			}
			any_realm_province = { religion = event_target:cr_marriage_offer_recipient }
		}
	}
}

# Completes only a still-valid settlement. No term has any effect before this
# effect is called from the recipient's Accept option.
cr_complete_negotiated_marriage_effect = {
	if = {
		limit = { cr_marriage_settlement_valid_trigger = yes }
		cr_apply_negotiated_religion_terms_effect = yes
		if = {
			limit = {
				event_target:cr_marriage_offer_author = {
					has_character_flag = cr_marriage_author_offers_vassalization
				}
			}
			event_target:cr_marriage_offer_author = {
				set_defacto_liege = event_target:cr_marriage_offer_recipient
			}
		}

		if = {
			limit = {
				event_target:cr_marriage_proposer = {
					same_sex = event_target:cr_marriage_candidate
				}
			}
			event_target:cr_marriage_candidate = {
				hidden_effect = { set_gender = opposite }
			}
			if = {
				limit = {
					event_target:cr_marriage_offer_author = {
						has_character_flag = cr_marriage_term_matrilineal
					}
				}
				event_target:cr_marriage_proposer = {
					add_spouse_matrilineal = event_target:cr_marriage_candidate
				}
			}
			else = {
				event_target:cr_marriage_proposer = {
					add_spouse = event_target:cr_marriage_candidate
				}
			}
			event_target:cr_marriage_candidate = {
				hidden_effect = { set_gender = opposite }
			}
		}
		else = {
			if = {
				limit = {
					event_target:cr_marriage_offer_author = {
						has_character_flag = cr_marriage_term_matrilineal
					}
				}
				event_target:cr_marriage_proposer = {
					add_spouse_matrilineal = event_target:cr_marriage_candidate
				}
			}
			else = {
				event_target:cr_marriage_proposer = {
					add_spouse = event_target:cr_marriage_candidate
				}
			}
		}

		event_target:cr_marriage_proposer = {
			set_character_flag = cr_negotiated_marriage_reward_cooldown
			character_event = { id = cr_marriage.102 days = 1825 }
		}
		event_target:cr_marriage_candidate = {
			set_character_flag = cr_negotiated_marriage_reward_cooldown
			character_event = { id = cr_marriage.102 days = 1825 }
		}

		if = {
			limit = { event_target:cr_marriage_proposer = { ai = no } }
			event_target:cr_marriage_proposer = { long_character_event = { id = cr_marriage.100 } }
		}
		else_if = {
			limit = { event_target:cr_marriage_candidate = { ai = no } }
			event_target:cr_marriage_candidate = { long_character_event = { id = cr_marriage.100 } }
		}
		else = {
			event_target:cr_marriage_proposer = { long_character_event = { id = cr_marriage.100 } }
		}
	}
	else = {
		if = {
			limit = { event_target:cr_marriage_offer_recipient = { ai = no } }
			event_target:cr_marriage_offer_recipient = { long_character_event = { id = cr_marriage.9 } }
		}
		else_if = {
			limit = { event_target:cr_marriage_offer_author = { ai = no } }
			event_target:cr_marriage_offer_author = { long_character_event = { id = cr_marriage.9 } }
		}
		else = { cr_clear_marriage_negotiation_effect = yes }
	}
}

# ROOT is the spouse receiving the ceremony event. The other spouse is saved as
# event_target:cr_marriage_partner. Rank is based on the more highly titled one.
cr_grant_negotiated_marriage_rewards_effect = {
	if = {
		limit = {
			OR = {
				tier = EMPEROR
				event_target:cr_marriage_partner = { tier = EMPEROR }
			}
		}
		prestige = 800
		event_target:cr_marriage_partner = { prestige = 800 }
		if = { limit = { is_landed = yes } wealth = 250 }
		if = { limit = { event_target:cr_marriage_partner = { is_landed = yes } } event_target:cr_marriage_partner = { wealth = 250 } }
		if = { limit = { is_landed = no event_target:cr_marriage_partner = { is_landed = no } } wealth = 125 event_target:cr_marriage_partner = { wealth = 125 } }
	}
	else_if = {
		limit = {
			OR = {
				tier = KING
				event_target:cr_marriage_partner = { tier = KING }
			}
		}
		prestige = 400
		event_target:cr_marriage_partner = { prestige = 400 }
		if = { limit = { is_landed = yes } wealth = 120 }
		if = { limit = { event_target:cr_marriage_partner = { is_landed = yes } } event_target:cr_marriage_partner = { wealth = 120 } }
		if = { limit = { is_landed = no event_target:cr_marriage_partner = { is_landed = no } } wealth = 60 event_target:cr_marriage_partner = { wealth = 60 } }
	}
	else_if = {
		limit = {
			OR = {
				tier = DUKE
				event_target:cr_marriage_partner = { tier = DUKE }
			}
		}
		prestige = 200
		event_target:cr_marriage_partner = { prestige = 200 }
		if = { limit = { is_landed = yes } wealth = 60 }
		if = { limit = { event_target:cr_marriage_partner = { is_landed = yes } } event_target:cr_marriage_partner = { wealth = 60 } }
		if = { limit = { is_landed = no event_target:cr_marriage_partner = { is_landed = no } } wealth = 30 event_target:cr_marriage_partner = { wealth = 30 } }
	}
	else_if = {
		limit = {
			OR = {
				tier = COUNT
				event_target:cr_marriage_partner = { tier = COUNT }
			}
		}
		prestige = 100
		event_target:cr_marriage_partner = { prestige = 100 }
		if = { limit = { is_landed = yes } wealth = 30 }
		if = { limit = { event_target:cr_marriage_partner = { is_landed = yes } } event_target:cr_marriage_partner = { wealth = 30 } }
		if = { limit = { is_landed = no event_target:cr_marriage_partner = { is_landed = no } } wealth = 15 event_target:cr_marriage_partner = { wealth = 15 } }
	}
	else = {
		prestige = 50
		event_target:cr_marriage_partner = { prestige = 50 }
		if = { limit = { is_landed = yes } wealth = 15 }
		if = { limit = { event_target:cr_marriage_partner = { is_landed = yes } } event_target:cr_marriage_partner = { wealth = 15 } }
		if = { limit = { is_landed = no event_target:cr_marriage_partner = { is_landed = no } } wealth = 10 event_target:cr_marriage_partner = { wealth = 10 } }
	}
}

cr_clear_marriage_ceremony_targets_effect = {
	if = {
		limit = { event_target:cr_marriage_guest_1 = { always = yes } }
		event_target:cr_marriage_guest_1 = { clr_character_flag = cr_marriage_ceremony_guest }
	}
	if = {
		limit = { event_target:cr_marriage_guest_2 = { always = yes } }
		event_target:cr_marriage_guest_2 = { clr_character_flag = cr_marriage_ceremony_guest }
	}
	clear_event_target = cr_marriage_guest_1
	clear_event_target = cr_marriage_guest_2
	clear_event_target = cr_marriage_partner
	cr_clear_marriage_negotiation_effect = yes
}
